package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvposDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.entity.YddLink;
import com.itheima.ydd.service.IYddLinkService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_link")
public class YddLinkController {

    @Autowired
    private IYddLinkService yddLinkService;

    @GetMapping("/list")
    public R page(Integer type, Integer page, Integer limit){
        //分页构造器
        Page<YddLink> pageInfo=new Page<>(page,limit);
        LambdaQueryWrapper<YddLink> lqw=new LambdaQueryWrapper<>();
        //将传入的参数添加进条件选择器
        lqw.eq(YddLink::getType,type);
        yddLinkService.page(pageInfo,lqw);

        R r=new R();
        r.putData("count",Integer.parseInt(String.valueOf(pageInfo.getTotal())));
        r.putData("totalPages",(int)Math.ceil((Double.valueOf(String.valueOf(pageInfo.getTotal()))/pageInfo.getSize())));
        r.putData("pageSize",Integer.parseInt(String.valueOf(pageInfo.getSize())));
        r.putData("currentPage",Integer.parseInt(String.valueOf(pageInfo.getCurrent())));
        r.putData("data",pageInfo.getRecords());

        r.setErrmsg("");
        r.setErrno(0);
        return r;
    }

    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        YddLink yddLink = yddLinkService.getById(id);
        return ResultData.ok("",yddLink);
    }


    @PostMapping("/update")
    public ResultData update(@RequestBody YddLink yddLink){
        yddLinkService.updateById(yddLink);
        return ResultData.ok("","");
    }

    @GetMapping("/del")
    public ResultData delete(Integer id){
        yddLinkService.removeById(id);
        return ResultData.ok("","");
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody YddLink yddLink){
        yddLinkService.save(yddLink);
        return ResultData.ok("","");
    }

}
